package cn.jwelll.bxcstock.ui.login

import android.text.Editable
import android.text.TextUtils
import android.text.TextWatcher
import android.view.KeyEvent
import android.view.View
import cn.jwelll.bxcstock.R
import cn.jwelll.bxcstock.base.BaseActivity
import cn.jwelll.bxcstock.base.MVPBaseActivity
import cn.jwelll.bxcstock.common.ConstValue
import cn.jwelll.bxcstock.util.ActivityUtil
import cn.jwelll.bxcstock.util.SPUtils
import cn.jwelll.bxcstock.util.ToastUtils
import kotlinx.android.synthetic.main.activity_login.*

class LoginActivity : MVPBaseActivity<LoginContract.View, LoginPresenter>(), LoginContract.View {

    private var mNumber: String? = null
    private var mPassWord: String? = null

    override fun createPresenter() = LoginPresenter()

    override fun getContentView() = R.layout.activity_login

    override fun getTitleString() = ""

    override fun loginSuccess(msg: String?) {
        dismissProgress()
        ActivityUtil.startMainActivity(this)
        finish()
    }

    override fun loginFailde(msg: String?) {
        dismissProgress()
        ToastUtils.showCenterToast(this, msg)
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean {
        // 过滤按键动作，按返回键不退出程序，直接退到桌面
        if (event.action == KeyEvent.ACTION_DOWN && keyCode == KeyEvent.KEYCODE_BACK) {
            moveTaskToBack(true)//true对任何Activity都适用
            return true
        }
        return super.onKeyDown(keyCode, event)
    }

    override fun initView() {
        super.initView()


        cb_login_remember.isChecked = SPUtils.getBoolean(this, ConstValue.AUTO_LOGIN, false)
        //自动登录&&登录状态
        if (SPUtils.getBoolean(this, ConstValue.AUTO_LOGIN, false) && SPUtils.getBoolean(
                this,
                ConstValue.LOGIN_STATE,
                false
            )
        ) {
            et_login_password.setText(SPUtils.getString(this, ConstValue.LOGIN_PASS, ""))
            mPassWord = SPUtils.getString(this, ConstValue.LOGIN_PASS, "")
            if (!TextUtils.isEmpty(mPassWord)) {
                iv_login_password_clear.visibility = View.VISIBLE
            }
        }
        et_login_number.setText(SPUtils.getString(this, ConstValue.LOGIN_COUNT, ""))
        mNumber = SPUtils.getString(this, ConstValue.LOGIN_COUNT, "")
        if (!TextUtils.isEmpty(mNumber)) {
            iv_login_number_clear.visibility = View.VISIBLE
        }
    }

    override fun initData() {
        super.initData()
    }

    override fun initEvent() {
        super.initEvent()
        et_login_number.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {

            }

            override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
                if (!TextUtils.isEmpty(et_login_number.getText().toString().trim { it <= ' ' })) {
                    iv_login_number_clear.visibility = View.VISIBLE
                } else {
                    iv_login_number_clear.visibility = View.GONE
                }
                mNumber = et_login_number.text.toString().trim { it <= ' ' }

            }

            override fun afterTextChanged(s: Editable) {

            }
        })
        et_login_password.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {

            }

            override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
                if (!TextUtils.isEmpty(et_login_password.getText().toString().trim { it <= ' ' })) {
                    iv_login_password_clear.visibility = View.VISIBLE
                } else {
                    iv_login_password_clear.visibility = View.GONE
                }
                mPassWord = et_login_password.text.toString().trim { it <= ' ' }
            }

            override fun afterTextChanged(s: Editable) {

            }
        })
        cb_login_remember.setOnCheckedChangeListener { _, isChecked ->
            SPUtils.putBoolean(
                this,
                ConstValue.AUTO_LOGIN,
                isChecked
            )
        }


        tv_login_login.setOnClickListener {
            if (TextUtils.isEmpty(mNumber)) {
                ToastUtils.showCenterToast(this, "账号不能为空！")
            } else if (TextUtils.isEmpty(mPassWord)) {
                ToastUtils.showCenterToast(this, "密码不能为空！")
            } else {
                showLoading("登录中。。。")
                mPresenter.login(mNumber, mPassWord)
            }
        }

        iv_login_password_clear.setOnClickListener {
            et_login_password.setText("")
        }
        iv_login_number_clear.setOnClickListener {
            et_login_number.setText("")
        }
    }
}